package com.cai.demo.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cai.demo.entity.User;
import com.cai.demo.entity.dto.LoginDTO;
import com.cai.demo.response.ResultVO;
import com.cai.demo.service.UserService;
import com.cai.demo.util.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.Map;
import java.util.Queue;
import java.util.Set;

/**
 * @Title: UserController
 * @Author RuoLi
 * @Package com.cai.demo.controller
 * @Date 2023/12/14 20:59
 * @description: 用户控制
 */
@RestController
@RequestMapping("user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("")
    public ResultVO getUser() {
        log.debug("获取用户账号密码");
        String token = JWTUtil.createToken("admin");
        return ResultVO.ok(token);
    }

    @GetMapping("/getPermission/{username}")
    public ResultVO getPermission(@PathVariable String username) {
        System.out.println(username);
        log.debug("获取用户权限");
        return ResultVO.ok(userService.getPermission(username));
    }

    @GetMapping("/login/{username}/{password}")
    public ResultVO login(@PathVariable String username,@PathVariable String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        wrapper.eq("password",password);
        int count = userService.count(wrapper);
        if (count>0){
            Set<Integer> permission = userService.getPermission(username);
            String jsonString = JSON.toJSONString(permission);
            String token = JWTUtil.createToken(jsonString);
            return ResultVO.ok(token);
        }
        return ResultVO.fail();
    }
}
